<?php
/*************************************************************************************
 * 设备管理
 * code（adddevice，device_name ，groupName，delete）
 * adddevice   ：新增设备
 * device_name ：更改设备别名
 * groupName   ：更改设备位置
 * delete      ：删除设备
 * 错误代码：
 *             ：
 *             ：
 *             ：
 *             ：
 *             ：
 *************************************************************************************/
$openid = $_POST["openid"];
$applianceId=$_POST["applianceId"];
$device_name=$_POST["device_name"];
$groupName=$_POST["groupName"];
$code=$_POST["code"];
        
switch ($code) {
    case 'adddevice':
        if(than_data($openid,$applianceId) == null){
            $a = new_light($openid,$applianceId,$device_name,$groupName);
            echo json_encode(judge($a));
        }else{
            echo json_encode(judge(1));
        }
        break;
    case 'device_name':
        $a = change_data($openid,"device_name",$device_name,$applianceId);
        echo json_encode(judge($a));
        break;
    case 'groupName':
        $a = change_data($openid,"groupName",$groupName,$applianceId);
        echo json_encode(judge($a));
        break;
    case 'delete':
        $a = delete_data($openid,$applianceId);
        echo json_encode(judge($a));
        break;
}
function judge($a){
    if($a == 0){
        $Success = array(
            "error"=>$a,
            "state"=>"success"
        );
        return $Success;
    }else{
        $failure = array(
            "error"=>$a,
            "state"=>"failure"  
        );
        return $failure;
    }
}
//更新数据
function change_data($openid,$listing,$value,$applianceId){
    $uer = find_data($openid);
    $conn = connect_database();
    $sql = "UPDATE $uer SET $listing='$value' WHERE applianceId='$applianceId'";
    mysqli_select_db( $conn, 'RUNOOB' );
    $retval = mysqli_query( $conn, $sql );
    if(! $retval ){
        //die('无法更新数据: ' . mysqli_error($conn).mysqli_affected_rows($conn));
        return mysqli_error($conn);
    }else{
        //echo '数据更新成功！'.mysqli_affected_rows($conn);
        return 0;
    }
    $conn->close();
}

//连接数据库
function connect_database($database="user_database"){
    $conn = new mysqli("localhost", "root", "1314", $database);
    if ($conn->connect_error) {// 检测连接
        die("内部错误： " . $conn->connect_error);
    }
    return $conn;
}
//查找数据
function find_data($openid){
    $conn = connect_database();
    $sql = "SELECT userlibrary FROM openidlibrary WHERE openid_wx='$openid'"; //SELECT=列  FPOM=表 WHERE=条件
    $result = mysqli_query($conn,$sql);  //查找数组
    if(! $result )
    {
        //echo '读取数据失败: ' . mysqli_error($conn);
    }else {
    $result = mysqli_fetch_assoc($result); //获取数组
    $dax = implode('',$result);//数组转字符
    }
    return $dax;
    $conn->close();
}
//插入数据
function increase_data($surface,$openid_wx){
    $uer = makeCardPassword();
    $uer = "yf".$uer;
    $conn = connect_database();
    $sql = "INSERT INTO $surface (openid_wx,openid_xa,openid_tm,openid_xd,openid_wx,userlibrary) 
    VALUES ($openid_wx,0,0,0,$uer)";
    $affected = mysqli_affected_rows($sql);
    if($affected!==1){
        return 1;
    }
    $conn->close();
}
//删除数据
function delete_data($openid,$applianceId){
    $uer = find_data($openid);
    $conn = connect_database();
    $sql = "DELETE FROM $uer WHERE applianceId='$applianceId'";
    mysqli_select_db( $conn, 'RUNOOB' );
    $retval = mysqli_query( $conn, $sql );
    if(! $retval ){
        //die('无法删除数据: ' . mysqli_error($conn));
        return mysqli_error($conn);
    }else{
        //echo '数据删除成功！';
        return 0;
    }
    $conn->close();
}

//比对applianceId
function than_data($openid,$applianceId){
    $uer = find_data($openid);
    $conn = connect_database();
    $sql = "SELECT id FROM $uer WHERE applianceId='$applianceId'"; //SELECT=列  FPOM=表 WHERE=条件
    $result = mysqli_query($conn,$sql);  //查找数组
    $result = mysqli_fetch_assoc($result); //获取数组
    if(!isset($result))
    {
        return null;
    }else {
        return 1;
    }
    
    $conn->close();
}


/********************************************************************************
 * 新增设备
 * applianceId    : 设备序列号
 * add_time       : 添加时间（系统默认）
 * device_name    : 设备别名
 * groupName      : 设备位置
 * applianceTypes : 设备类型 （LIGHT）
 * devices_value  : 设备状态 （OFF）
 * image_1        : 微信设备初始图标（'../../png/denghong.png'）
 * image_2        : 微信设备初始网络图标（'../../png/wifi_wu.png'）
 * lamp           : 微信操作函数（onClick_open）
 * device_time    : 设备上报状态时间（初始值：0）
 ********************************************************************************/
function new_light($openid,$applianceId,$device_name,$groupName){
    date_default_timezone_set('PRC');
    $time = date("YmdHis");
    $uer = find_data($openid);
    $conn = connect_database();
    $sql = "INSERT INTO $uer (applianceId,add_time,device_name,groupName,applianceTypes,devices_value,image_1,image_2,lamp,device_time) 
    VALUES ('$applianceId','$time','$device_name','$groupName','LIGHT','OFF','..\/..\/png\/denghong.png','..\/..\/png\/wifi_wu.png','onClick_open',0)";
    mysqli_select_db( $conn, 'RUNOOB' );
    $retval = mysqli_query( $conn, $sql );
    if(!$retval){
        //die('无法插入数据: ' . mysqli_error($conn));
        return mysqli_error($conn);
    }
    //echo "数据插入成功\n";
    return 0;
    $conn->close();
}





?>